--******************************************************************************
-- File: kb_interface.vhd
--
-- Entity: kb_interface
-- Architecture: def
-- Author: Gabe Ferencz
-- Created: 10/07/09
-- Modified: 
-- VHDL'93
-- Description: empty template for keyboard interface
--******************************************************************************
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.all;
USE IEEE.STD_LOGIC_ARITH.all;
USE IEEE.STD_LOGIC_SIGNED.all;

--******************************************************************************
-- Entity Declaration
--******************************************************************************
ENTITY kb_interface IS
	PORT(	
		SIGNAL keyboard_clk 						: IN std_logic;
		SIGNAL keyboard_data 					: IN std_logic;
		SIGNAL clock_100Mhz	 					: IN std_logic;
		SIGNAL led_l, led_r, led_u, led_d	: OUT std_logic
	);
END kb_interface;

--******************************************************************************
-- Architecture def
--****************************************************************************** 
architecture def of kb_interface is
	----------------------------------------------------------
	-- keyboard.vhdl
	----------------------------------------------------------
	COMPONENT keyboard IS
		PORT(	
			keyboard_clk, keyboard_data, clock_25Mhz , 
			reset, read1		: IN	STD_LOGIC;
			scan_code		: OUT	STD_LOGIC_VECTOR(7 DOWNTO 0);
			scan_ready		: OUT	STD_LOGIC
		);
	END COMPONENT;
signal 		sReset:std_logic;
Signal      sRead:std_logic;
signal      sScan_code:std_logic_vector(7 downto 0);
signal      sScan_ready:std_logic;

type keycode is (up,dwn,np);

signal kCode:keycode:=np;

type state is (s0,s1,s2);

signal sState:state:=s0;

begin
	--------------------------------------------------------
	-- KEYBOARD module.  Low level serial-->parallel unit
	--------------------------------------------------------	
	KB: keyboard
		PORT MAP(
			keyboard_clk 	=> keyboard_clk,
			keyboard_data	=> keyboard_data,
			clock_25Mhz		=> clock_100Mhz,
			reset				=> sReset,
			read1				=> sRead,
			scan_code		=> sScan_code,
			scan_ready		=> sScan_ready	);
	
	--keyboard process......

process(sScan_ready,clock_100mhz,sReset,sState)

--type state is (s0,s1,s2,s3,s4);

--signal sState:state;


begin
	
if(clock_100Mhz'event and clock_100mhz='1') then
sRead<=sScan_ready;


if(sScan_ready='1') then
 
 if(sReset='1') then
   sState<=s0;
	
else 
	case sState is
	--state zeor accepts E0
	when s0 =>
	led_l<='1';
	led_r<='1';
	led_u<='1';
	led_d<='1';
	kCode:=np;
	if(sScan_code="11100000") then
	sState<=s1;
	else
	sState<=s0;
	end if;
	
	when s1=>
	---checks if the the NXT code is F0 is F0 then its not a make code any more it has changed state
	if(sScan_code="11110000")then
	sState<=s2;--change state


	--key press UP ON
	elsif(sScan_code="01110101")then
	led_l<='0';
	led_r<='0';
	led_u<='1';
	led_d<='1';
	kCode:=up;
	sState<=s0;--going HOME
	--key press DOWN ON
	elsif(sScan_code="01110010")then
	led_l<='1';
	led_r<='1';
	led_u<='0';
	led_d<='0';
	sState<=s0;
	kCode:=dwn;
	end if;
	
	
---checks if the the NXT code is E0..cool i for a BREAK :-)
	when s2=>
	--key Press UP OFF
	if(sScan_code="01110101")then
	led_l<='1';
	led_r<='1';
	sState<=s0;--going HOME

	--key press DOWN OFF
	elsif(sScan_code="01110010")then
	led_u<='1';
	led_d<='1';
	sState<=s0;

	end if;
	
	End case;
	
		end if;
	end if;
	end if;
end process;
end def;